package cn.deu.xunwu;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.dzu.dao.XunwuDao;
import cn.dzu.entity.Xunwu;

/**
 * Servlet implementation class loginservlet
 */
@WebServlet("/loginservlet")
public class loginservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		    String id1 = request.getParameter("id"); 
		    int id= Integer.parseInt(id1);
	        String password = request.getParameter("pass");  
	       ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		   XunwuDao xunwu=(XunwuDao)applicationContext.getBean("XunwuDao");	
		    	
		   List<Xunwu> list= xunwu.login(id,password);
		   if(list==null||list.size()==0) {
			   response.setContentType("text/html;charset=UTF-8");
			   PrintWriter out=response.getWriter();	
			   out.print("<script>alert('账户和或者密码错误');window.location.href('login.jsp')   </script>");
		   }else {
			   request.getRequestDispatcher("/select").forward(request, response);
		   }		
	}

}
